package pluginMain;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bukkit.command.CommandSender;

public class CommandColdDownManager {
	public static class ExecutedCommand {
		Long Time;
		CommandSender sender;
		String Command;

		public ExecutedCommand(Long _time, CommandSender _sender,
				String _command) {
			Time = _time;
			sender = _sender;
			Command = _command;
		}
	}

	private static List<ExecutedCommand> list = new ArrayList<ExecutedCommand>();

	private static Long getTime() {
		return Long.parseLong((new SimpleDateFormat("yyyyMMddHHmmssSSS"))
				.format((new Date(System.currentTimeMillis()))));
	}

	private static void addCommander(CommandSender sender, String command) {
		list.add(new ExecutedCommand(getTime(), sender, command));
	}

	public static boolean test(CommandSender sender, String command) {
		for (ExecutedCommand c : list) {
			//exist 
			if (c.Command.equals(command) && c.sender.equals(sender)) {
				if (getTime() - c.Time < 5) {
					return false;
				} else {
					c.Time = getTime();
					return true;
				}
			}
		}
		//not any exist in list
		addCommander(sender , command);
		return true;
	}
}
